/**
 * 
 */

 var cfgGrids={
	    		provincecfg:{
	    			title:"企业号管理",
	    			cols: {
	    				corpId:{name:"企业号CORPID",type:"string",show:true},
	    				province:{name:"名称标识",type:"string",show:true},// 只能采用隐藏的方式
	    				provincename:{name:"企业号描述",type:"string",show:true,width:"5%"},
	    				corpSecret:{name:"CORPSECRET",type:"string",show:true,width:"20%"},
	    				aesKey:{name:"AESKEY",type:"string",show:true,width:"20%"},
	    				used:{name:"启用",type:"bool",show:true,width:"3%"},
	    				url:{name:"后台URL",type:"string",show:true},
	    				corpicon:{name:"企业号图标",type:"string",show:true},
	    				extra:{name:"应用",show:true,width:"5%",content:"<span class='extra' style='width:5%;'><i  class='fa fa-cubes fa-lg'></i></span>"}//扩展编辑字段并不存入数据库
	    			}
	    		}
  };
	    

  $(function($){
//	    	  window.onresize = function(){
//	   		  if($("body").width()<1210)
//	   			  $("body").width(1210);
//	   		  
//	   	          adjustwindow();
//              }
	    	 
	    	 
		     adjustwindow();
		   	 function   adjustwindow(){
		     	$(".dgride .content").height($(".dgride").height()-$(".dgride .title").height()-5);
		    	$(".dgride").width($(".ds_view").width());
		    	
		   	 } 
		    
		    
		
 
 	});
  function adjustScroll(){
	  
	//==============
  	var tar=$(".dgride .scroll")[0];
		 if(tar.style.overflow!="hidden"&&tar.scroll!="no"&&
				 tar.scrollHeight>tar.offsetHeight)
				{
				 alert("有滚动条");
				}else 
				{ 
				 alert("无滚动条");
				}
  }
	    
	  
	    
	    $.fn.dgrid =function(options){
	    	 
	    	  var Grid = function(ele, opt) {
	    	    this.gd=ele;;
	  	        this.rows= ele.find(".items");
	  	        this.header=ele.find(".gbheader");
	  	        this.defaults = { 
	  	        		gdcfg:cfgGrids.provincecfg,geturl:CONETXT_PATH+"/super/provincejson/",saveurl:CONETXT_PATH+"/super/saveprivince/"
	  	        };
	  	        this.options = $.extend({}, this.defaults, opt);
	  	      
	  	     
	  	      //注册基本事件
		        $("body").on("click",  "#"+this.gd.attr("id")+"  .items .item" ,function(){
			    	 var tar=$(this);
			    	 tar.siblings().removeClass("active");
			    	if( tar.hasClass("active")){
			    		tar.removeClass("active");
			    	}else{
			    		tar.addClass("active");
			    	}
			    });
			    
			    /*
			           切换行的边界状态
			    */
				$("body").on("click", "#"+this.gd.attr("id")+" .items .item .edit" ,function(){
					  var tar=$(this).parent();
					  var flg="readonly";
					  if(tar.attr(flg)){
						  endEdit(tar.siblings());//同时只能有一个处于编辑状态,关闭其他的编辑状态
						  beginEdit(tar);  
						  
					  }else{
						  endEdit(tar);
			 
					  }
					  
					  function endEdit(tar){
						  tar.attr(flg,flg);
						  tar.children().attr(flg,flg);
						  tar.children(".edit").attr("class","edit fa fa-edit");
						  tar.removeClass("editable");	
						  tar.children("input:checkbox").attr("onclick","return false");
					  }
					  function beginEdit(tar){
						  tar.removeAttr(flg);
						  tar.children().removeAttr(flg); 
						  tar.children(".edit").attr("class","edit fa fa-check-square-o");
						  tar.addClass("editable");
						  tar.children("input:checkbox").removeAttr("onclick");
					  }
				 }); 
			    
	    	  };
	  	     //根据数据生成行数据
	    	  var addRowItem=function( data,cols){
	    			 
				    	var row="<li class='item' readonly=readonly>"
					 	$.each(data,function(key,value){
					 		var type ='text';
					 		var width=cols[key].width;
					 		var widthstyle="style='width: "+width+";'"

                              if(!cols[key].show) return;
					 		  if(cols[key].type=="bool")
					 			{
					 			   type="checkbox";
					 			   var checked=(value==true)?"checked":"";
					 			   row+= "<input class='"+key+"' type='"+type+"' "+widthstyle+"  "+checked+"    onclick='return false'>";
					 			}else
						 		 {
						 				row+= "<input class='"+key+"' type='  "+type+"' value='"+value+"' "+widthstyle+" readonly   >";
						 		 }
					 	});
				    	
					 	row+=cols["extra"].content+"<i class=' edit fa fa-edit'></i> </li>"
					 	return row;
			  };
	    	//定义Beautifier的方法
	    	  Grid.prototype = {
	    			     /**
					                      添加新行
						 */
						 addRowItem: function(data){
							 var tar= this.rows;
							 var options=this.options;
							 tar.append(addRowItem(data,options.gdcfg.cols));
							 },
						 init:function(){
						      var gdrows=  this.rows;
						      var options=this.options;
						      var header=this.header;
						
							 //初始化 
				              
					          $.getJSON(options.geturl, function(json){
						      		// 使用模版
						         	var rows="";
						  	        $.each(json, function(index,rowdata) {
						  	        	rows+= addRowItem(rowdata,options.gdcfg.cols);
						  	        });
						  	  
						  	      gdrows.html(rows);
						  	     initheader(header);
						     
						     });
							 
					          //初始化 header
							 function initheader(gdheader){
 
					       
							 var row="";
							 
							 $.each(options.gdcfg.cols,function(self,item){
								 if(item.show) 
								    gdheader.append("<span class='"+self+"'>"+item.name+"</span>") ;
								// row+="<span class='"+self+"'>"+item.name+"</span>";
								 if(item.width)
								 $("."+self).width(item.width);
							 });
							 }
							// gdheader.html(row);
							 
						 },
						save:function(){
							 var tar= this.rows;
						     var options=this.options;
						     var items=tar.children(".item");
						     var  list=[];
						     items.each(function(){
						    	  var item=$(this);
						    	  var inputs=item.children("input");
						    	  
						    	  var itemdata={};
						    	  inputs.each(function(){
						    		  var _tar=$(this);
						    		  if(_tar.attr("type")=="checkbox")
						    			  itemdata[_tar.attr("class")]=  Number(_tar[0].checked);
						    		  else
						    		      itemdata[_tar.attr("class")]=_tar.val();
						    	  });
						    	  list.push(itemdata);
						    	  
						    	 
						     });
						      
						     //提交保存
							   $.ajax({ 
						            type:"POST", 
						            url:options.saveurl, 
						            dataType:"json",      
						            contentType:"application/json",               
						            data: JSON.stringify(list), 
						            success:function(data){ 
						            	//显示保存结果
						            	showTipInfo(data);         
						            } 
						         }); 
						}
	    	  }
	     //新建表格类	
	     var gd =new Grid(this, options);
	     gd.init();//初始化表格
         return gd ;  
      };
      

	  $.extend({
		    log: function(message) {
		        var now = new Date(),
		            y = now.getFullYear(),
		            m = now.getMonth() + 1, //！JavaScript中月分是从0开始的
		            d = now.getDate(),
		            h = now.getHours(),
		            min = now.getMinutes(),
		            s = now.getSeconds(),
		            time = y + '/' + m + '/' + d + ' ' + h + ':' + min + ':' + s;
		        console.log(time + ' My App: ' + message);
		    }
	 });
	  
   //$.log('initializing...'); //调用  